package com.example.vdmtest;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.vivo.ic.VLog;
import com.vivo.ic.dm.DMUtil;
import com.vivo.ic.dm.DownloadWorker;
import com.vivo.ic.dm.NetDealHelper;

/**
 * 提示是否在移动网络下继续下载
 * 
 * @author wangzhenyu
 *
 */
public class MobileAlertActivity extends Activity {
    private static final String TAG = MainApplication.PRE_TAG + "VivoNetChangeAlertActivity";
    private boolean mIsNeverRemind;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mIsNeverRemind = DMUtil.isNeverRemind();
        
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.switch_network);
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.mobile_alert_dialog, null);
        
        TextView tv = (TextView) view.findViewById(R.id.tv);
        tv.setText(getString(R.string.wlan_disconnected_confirm));
        
        /**
         * 关于“不再提示”： 只有当用户点击OK按钮时，这个checkbox的值才会生效，其他情况维持原状不变。
         */
        CheckBox box = (CheckBox) view.findViewById(R.id.no_more_warning);
        box.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                VLog.i(TAG, "onCheckedChanged isChecked " + isChecked);
                mIsNeverRemind = isChecked;
            }
        });
        builder.setView(view);
        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                VLog.i(TAG, "on ok button clicked");
                DownloadWorker.runOnWorkerThread(new Runnable() {

                    @Override
                    public void run() {
                        DMUtil.setNeverRemind(mIsNeverRemind);
                        NetDealHelper.startDownloadNetPaused(getContentResolver());
                    }
                });
                
            }
        });
        
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                VLog.i(TAG, "on cancel button clicked");
            }
        });
        
        AlertDialog dialog = builder.create();
        dialog.show();
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            public void onDismiss(DialogInterface dialog) {
                VLog.i(TAG, "on dialog dismiss");
                finish();
            }
        });
        
    }
    
}
